import React, {Component} from 'react';
import './App.css';
import {Layout} from "antd";
import ApsHeader from "./header/ApsHeader";
import Lng from "./common/lng";
import axios from 'axios';
import ModalTips from "./common/ModalTips";
import {HashRouter as Router} from "react-router-dom";
import IndexRouter from "./router/MenuRouter";

const {Content, Footer} = Layout;

class App extends Component {
    constructor(props) {
        super(props);

        Lng.init("zh-CN");
        axios.defaults.baseURL = "http://localhost:9000/aps";

        const self = this;

        axios.interceptors.request.use((config) => {
            self.error = false;
            return config;
        });

        //添加一个响应拦截器
        axios.interceptors.response.use((res) => {
            return res.data;
        }, (err) => {
            if (self.error) return;
            self.error = true;
            if (err.code === 'ECONNABORTED') { //超时
                ModalTips.error({
                    content: Lng.get("common.timeout")
                })
                return Promise.reject();
            }
            var status = err.response.status;
            var data = err.response.data;
            if (status === 400 && !data.success) {
                ModalTips.error({
                    content: data.message ? data.message : Lng.get("common.serverError")
                })
                return Promise.reject();
            }
        })
    }

    render() {
        return (
            <Router>
                <Layout className="layout">
                    <ApsHeader/>
                    <Content style={{padding: '50px 50px'}}>
                        <div style={{background: '#fff', padding: 24, minHeight: 500}}>
                            <IndexRouter/>
                        </div>
                    </Content>
                    <Footer style={{textAlign: 'center'}}>
                        Ant Design ©2016 Created by Ant UED
                    </Footer>
                </Layout>
            </Router>
        );
    }
}

export default App;
